<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="default">
    <PropertyGroup>
        <Configuration Condition="'$(Configuration)'==''" >Release</Configuration>

        <BaseDir>$(MSBuildProjectDirectory)</BaseDir>
        <BuildDir>$(BaseDir)\Releases</BuildDir>
        <NuGetSpecDir>$(BaseDir)</NuGetSpecDir>
        <NuGetSpecTempDir>$(BaseDir)\temp\</NuGetSpecTempDir>
        <SolutionDir>$(BaseDir)\..\Source\Core\Trunk\</SolutionDir>
        <SolutionFile>$(SolutionDir)IslandUnit.sln</SolutionFile>
        <SolutionDirBin>$(SolutionDir)\IslandUnit\bin\$(Configuration)\</SolutionDirBin>

        <SamplesDir>$(BaseDir)\..\Source\Samples\</SamplesDir>
    </PropertyGroup>

    <Import Project="$(BaseDir)\Lib\MSBuild.Community.Tasks.targets" />
    <UsingTask AssemblyFile="$(BaseDir)\Lib\NuGet.MSBuild.dll" TaskName="NuGet.MSBuild.NuGet" />

    <Target Name="default" DependsOnTargets="Compile; Copy; Samples; Package; Clean" />

    <Target Name="Compile">
        <MSBuild Projects="$(SolutionFile)" Properties="Configuration=$(Configuration)"  />
    </Target>

    <Target Name="Copy">
        <GetAssemblyIdentity AssemblyFiles="$(SolutionDirBin)\IslandUnit.dll">
            <Output TaskParameter="Assemblies" ItemName="AsmInfo" />
        </GetAssemblyIdentity>
        <ItemGroup>
            <MainBinaries Include="$(SolutionDirBin)**\*.dll" />
        </ItemGroup>
        <Copy SourceFiles="@(MainBinaries)" DestinationFolder="$(BuildDir)\%(AsmInfo.Version)" />
        <Copy SourceFiles="@(MainBinaries)" DestinationFolder="$(NuGetSpecTempDir)bin" />
        <Copy SourceFiles="$(NuGetSpecDir)\IslandUnit.nuspec" DestinationFolder="$(NuGetSpecTempDir)" />
    </Target>

    <Target Name="Samples">
        <GetAssemblyIdentity AssemblyFiles="$(SolutionDirBin)\IslandUnit.dll">
            <Output TaskParameter="Assemblies" ItemName="AsmInfo" />
        </GetAssemblyIdentity>
        <ItemGroup>
            <SamplesSource Include="$(SamplesDir)\Sample1\**\*.*" Exclude="$(SamplesDir)\Sample1\**\bin\**;$(SamplesDir)\Sample1\**\obj\**;$(SamplesDir)\Sample1\**\TestResults\**;$(SamplesDir)\Sample1\**\_ReSharper*\**;$(SamplesDir)\Sample1\**\*.vspscc;$(SamplesDir)\Sample1\**\*.vssscc;$(SamplesDir)\Sample1\**\*.suo;$(SamplesDir)\Sample1\**\*.user" />
            <MainBinaries Include="$(SolutionDirBin)**\*.dll" />
        </ItemGroup>
        <PropertyGroup>
            <BuildDirVersion>$(BuildDir)\%(AsmInfo.Version)\</BuildDirVersion>
        </PropertyGroup>
        <Copy SourceFiles="@(SamplesSource)" DestinationFolder="$(BuildDirVersion)Samples\Sample1\%(RecursiveDir)" />
        <Copy SourceFiles="@(MainBinaries)" DestinationFolder="$(BuildDirVersion)Samples\Sample1\packages\IslandUnit.%(AsmInfo.Version)\lib" />
    </Target>

    <Target Name="Package">
        <GetAssemblyIdentity AssemblyFiles="$(SolutionDirBin)\IslandUnit.dll">
            <Output TaskParameter="Assemblies" ItemName="AsmInfo" />
        </GetAssemblyIdentity>

        <XmlUpdate  Namespace="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"
								XmlFileName="$(NuGetSpecTempDir)IslandUnit.nuspec"
								XPath="/package/metadata/version"
								Value="%(AsmInfo.Version)" />

        <Message Text="Creating the package"/>
        <NuGet PackageDir="$(BuildDir)\" SpecFile="$(NuGetSpecTempDir)IslandUnit.nuspec" />
    </Target>

    <Target Name="Clean">
        <RemoveDir Directories="$(NuGetSpecTempDir)" />
    </Target>
</Project>
